package cn.itsource.org.controller;

import cn.itsource.basic.util.JsonResult;
import cn.itsource.order.domain.ParamDo;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Product;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.query.ProductQuery;
import cn.itsource.org.service.IProductService;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.query.PetQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * department控制层
 */
@RestController
@RequestMapping("/product")
@Api(tags = "产品管理")
public class ProductController {
    @Autowired
    private IProductService productService;
    @PostMapping("/pageList")
    public JsonResult pageList(@RequestBody ProductQuery productQuery){
        return JsonResult.createSuccess(productService.findPage(productQuery));
    }
    @PostMapping("/pageFList")
    public JsonResult pageFList(@RequestBody ProductQuery productQuery){
        return JsonResult.createSuccess(productService.findFPage(productQuery));
    }
    @GetMapping("/{id}")
    public JsonResult petDetail(@PathVariable Long id){
        return JsonResult.createSuccess(productService.petDetail(id));
    }
    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除操作",notes = "根据id删除部门")
    public JsonResult delete(@PathVariable("id") Long id){

        //根据id区分,没有id就是新增
        try {
            //调用删除
            productService.delete(id);

            return JsonResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return JsonResult.createError("操作异常,联系管理员李裕");
        }
    }
    @PostMapping
    @ApiOperation(value = "新增修改",notes = "可以根据id区分操作")
    public JsonResult saveOrUpdate(@RequestBody Product product) {

        //根据id区分,没有id就是新增
        try {
            productService.saveOrUpdate(product);
            return JsonResult.createSuccess();
        } catch (RuntimeException e) {
            e.printStackTrace();
            return JsonResult.createError(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.createError("操作异常,联系管理员李裕");
        }
    }
    @PostMapping("batchShelve")
    public JsonResult batchRemove(@RequestBody ProductQuery query){

        //根据id区分,没有id就是新增
        try {
            //调用删除
            Long[] ids = query.getIds();
            for (Long id : ids) {
                productService.petShelve(id);
            }
            return JsonResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return JsonResult.createError("操作异常,联系管理员李裕");
        }
    }
    @PostMapping("downShelve")
    public JsonResult batchDownShelve(@RequestBody ProductQuery query){

        //根据id区分,没有id就是新增
        try {
            //调用删除
            Long[] ids = query.getIds();
            for (Long id : ids) {
                productService.downShelve(id);
            }
            return JsonResult.createSuccess();
        } catch (Exception e) {

            e.printStackTrace();
            return JsonResult.createError("操作异常,联系管理员李裕");
        }
    }
    @PostMapping("price")
    @ResponseBody
    public JsonResult getPrice(@RequestBody ParamDo paramDo){
        Product price = productService.getPrice(paramDo);
        return JsonResult.createSuccess(price);
    }
    @PostMapping("submitOrder")
    @ResponseBody
    public JsonResult submitOrder(@RequestBody ParamDo paramDo){
        return productService.submitOrder(paramDo);
    }
}





